//
//  ZWCollectViewController.m
//  wan
//
//  Created by apple on 16/2/22.
//  Copyright © 2016年 MyCompany. All rights reserved.
//

#import "ZWCollectViewController.h"
#import "UIBarButtonItem+CH.h"
#import "ZWHeader.h"
#import "ZWCollectScenicCell.h"
#import "ZWSelectCell.h"

@interface ZWCollectViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *scenicView;
@property (nonatomic, strong) UITableView *featureView;
@property (nonatomic, strong) UIButton *scenicButton;
@property (nonatomic, strong) UIButton *featureButton;
@property (nonatomic, strong) NSArray *collectScenics;
@property (nonatomic, assign) NSIndexPath *selectedindexPath;
@property (nonatomic, strong) NSMutableArray *contacts;
@property (nonatomic, strong) UIButton *selectAllButton;
@property (nonatomic, strong) UIButton *editButton;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger i;
@end

@implementation ZWCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置导航栏
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"返回" target:self action:@selector(returnClick)];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"顶部"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithWhite:0.1 alpha:1.0], NSFontAttributeName:[UIFont boldSystemFontOfSize:15]};
    
    //设置全部按钮
    [self setupAllButtons];
    
    self.contacts = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:@"NO" forKey:@"checked"];
        [self.contacts addObject:dic];
    }
    
    self.dataArray = [[NSMutableArray alloc] init];
    for (int i= 0; i < 20; i++) {
        [self.dataArray addObject:[NSNumber numberWithInt:i + 1]];
    }
    
    //设置tableview
    self.scenicView = [[UITableView alloc] initWithFrame:CGRectMake(0, 148, WIDTH, 600) style:UITableViewStylePlain];
    self.scenicView.dataSource = self;
    self.scenicView.delegate = self;
    self.scenicView.rowHeight = 67;
    [self.view addSubview:self.scenicView];
    
    self.featureView = [[UITableView alloc] initWithFrame:CGRectMake(0, 148, WIDTH, 600) style:UITableViewStylePlain];
    self.featureView.hidden = YES;
    self.featureView.dataSource = self;
    self.featureView.delegate = self;
    self.featureView.rowHeight = 67;
    [self.view addSubview:self.featureView];
}

- (void)setupAllButtons
{
    //设置按钮
    self.scenicButton = [[UIButton alloc] initWithFrame:CGRectMake(60, 64, 80, 40)];
    [self.scenicButton setTitle:@"收藏的景区" forState:UIControlStateNormal];
    [self.scenicButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.scenicButton setTitleColor:[UIColor colorWithRed:255.0 / 255 green:88.0 / 255 blue:0 / 255 alpha:1.0] forState:UIControlStateSelected];
    self.scenicButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.scenicButton addTarget:self action:@selector(scenicClick:) forControlEvents:UIControlEventTouchUpInside];
    self.scenicButton.selected = YES;
    [self.view addSubview:self.scenicButton];
    
    self.featureButton = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 140, 64, 80, 40)];
    [self.featureButton setTitle:@"收藏的景点" forState:UIControlStateNormal];
    [self.featureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.featureButton setTitleColor:[UIColor colorWithRed:255.0 / 255 green:88.0 / 255 blue:0 / 255 alpha:1.0] forState:UIControlStateSelected];
    self.featureButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.featureButton addTarget:self action:@selector(featureClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.featureButton];
    
    //设置分隔线
    UIImageView *sep = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"分割线"]];
    sep.frame = CGRectMake(WIDTH / 2, 75, 1, 23);
    [self.view addSubview:sep];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 108, WIDTH, 40)];
    topView.backgroundColor = [UIColor colorWithRed:248.0 / 255 green:248.0 / 255 blue:248.0 / 255 alpha:1.0];
    [self.view addSubview:topView];
    
    //设置播放按钮
    self.playButton = [[UIButton alloc] initWithFrame:CGRectMake(13, 14, 14, 14)];
    [self.playButton setImage:[UIImage imageNamed:@"播放全部"] forState:UIControlStateNormal];
    [self.playButton setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateSelected];
    [self.playButton addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.playButton];

    //设置播放全部按钮
    self.selectAllButton = [[UIButton alloc] initWithFrame:CGRectMake(33, 11, 51, 21)];
    [self.selectAllButton setTitle:@"播放全部" forState:UIControlStateNormal];
    [self.selectAllButton setTitle:@"全选" forState:UIControlStateSelected];
    self.selectAllButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.selectAllButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.selectAllButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.selectAllButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    self.selectAllButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.selectAllButton addTarget:self action:@selector(selectAllClick:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.selectAllButton];
    
    //设置编辑按钮
    self.editButton = [[UIButton alloc] initWithFrame:CGRectMake(331, 13, 19, 14)];
    [self.editButton setImage:[UIImage imageNamed:@"Group Copy"] forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"完成 copy"] forState:UIControlStateSelected];
    [self.editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.editButton];
}

- (void)returnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)playClick:(UIButton *)sender
{
    
}

- (void)selectAllClick:(UIButton *)sender
{
    NSLog(@"播放全部");
}

- (void)editClick:(UIButton *)sender
{
    self.editButton.selected = !self.editButton.isSelected;
    
    if (self.editButton.selected) {
  //      NSLog(@"编辑");
        
        self.selectAllButton.selected = YES;
        self.playButton.selected = YES;
        [self.selectAllButton addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
        self.scenicView.frame = CGRectMake(50, 148, WIDTH, 600);
        
    } else {
        self.selectAllButton.selected = NO;
        self.playButton.selected = NO;
        [self.selectAllButton setTitle:@"播放全部" forState:UIControlStateNormal];
    }
}

- (void)selectClick:(UIButton*)sender
{
    NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.scenicView indexPathsForVisibleRows]];
    for (int i = 0; i < [anArrayOfIndexPath count]; i++) {
        NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];
        ZWSelectCell *cell = (ZWSelectCell*)[self.scenicView cellForRowAtIndexPath:indexPath];
        NSUInteger row = [indexPath row];
        NSMutableDictionary *dic = [self.contacts objectAtIndex:row];
        if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }
    if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]){
        for (NSDictionary *dic in (self.contacts)) {
            [dic setValue:@"YES" forKey:@"checked"];
        }
        [(UIButton *)sender setTitle:@"取消" forState:UIControlStateNormal];
    } else {
        for (NSDictionary *dic in (self.contacts)) {
            [dic setValue:@"NO" forKey:@"checked"];
        }
        [(UIButton*)sender setTitle:@"全选" forState:UIControlStateNormal];
    }
}


- (void)scenicClick:(UIButton *)button
{
    self.scenicView.hidden = NO;;
    self.featureView.hidden = YES;
    
    self.scenicButton.selected = YES;
    self.featureButton.selected = NO;
}

- (void)featureClick:(UIButton *)button
{
    
    self.scenicView.hidden = YES;
    self.featureView.hidden = NO;
    
    self.scenicButton.selected = NO;
    self.featureButton.selected = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.scenicView) {
        return 10;
    } else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.scenicView){
        static NSString *scenicID = @"scenic";
        ZWCollectScenicCell *cell = [tableView dequeueReusableCellWithIdentifier:scenicID];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ZWCollectScenicCell" owner:nil options:nil] lastObject];
        }
        ZWCollectScenic *collectScenic = self.collectScenics[indexPath.row];
        cell.collectScenic = collectScenic;
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 16, 4)];
        [button setImage:[UIImage imageNamed:@"Oval 29 + Oval 29 Copy + Oval 29 Copy 2 Copy Copy Copy Copy Copy Copy Copy"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(unfoldClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = button;
        
        return cell;
        
//        if (self.selectAllButton.selected) {
//            static NSString * identifier = @"Cell";
//            ZWSelectCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//            if (cell == nil) {
//                cell = [[ZWSelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//            }
//            
//            NSUInteger row = [indexPath row];
//            NSMutableDictionary *dic = [self.contacts objectAtIndex:row];
//            if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
//                [dic setObject:@"NO" forKey:@"checked"];
//                [cell setChecked:NO];
//                
//            }else {
//                [dic setObject:@"YES" forKey:@"checked"];
//                [cell setChecked:YES];
//            }
//        }
//        [self.scenicView reloadData];
//        return cell;
    } else {
        static NSString *featureID = @"feature";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:featureID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:featureID];
        }
        
        return cell;
    }
}

- (void)unfoldClick:(UIButton *)sender
{
    ZWCollectScenicCell *cell = (ZWCollectScenicCell *)[sender superview];
    self.selectedindexPath = [self.scenicView indexPathForCell:cell];
    NSLog(@"indexPath------%ld", (long)self.selectedindexPath.row);
    
    
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithRed:248.0 / 255 green:248.0 / 255 blue:248.0 / 255 alpha:1.0];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.i = indexPath.row;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZWSelectCell *cell = (ZWSelectCell*)[tableView cellForRowAtIndexPath:indexPath];
    
    NSUInteger row = [indexPath row];
    NSMutableDictionary *dic = [self.contacts objectAtIndex:row];
    if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
        [dic setObject:@"YES" forKey:@"checked"];
        [cell setChecked:YES];
    }else {
        [dic setObject:@"NO" forKey:@"checked"];
        [cell setChecked:NO];
    }
}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.row == self.selectedindexPath.row) {
//        return 120;
//    } else {
//        return 50;
//    }
//}

@end